from flask import request, current_app
from urllib.parse import urlparse, urljoin
from app.config import Config
import oss2


# 获取oss2
def get_oss_bucket():
    auth = oss2.Auth(Config.OSS_ACCESS_KEY_ID, Config.OSS_ACCESS_KEY_SECRET)
    return oss2.Bucket(auth, Config.OSS_END_POINT, Config.OSS_BUCKET)

oss_bucket = get_oss_bucket()


# 验证跳转URL是否安全
def is_safe_url(target):
    ref_url = urlparse(request.host_url)
    test_url = urlparse(urljoin(request.host_url, target))
    return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc
